package com.twh.www.service;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Set;

@Service
public class SmsService {
    // 生产环境请求地址
    private static final String SERVER_IP = "app.cloopen.com";
    // 请求端口
    private static final String SERVER_PORT = "8883";
    // 主账号
    private static final String ACCOUNT_SID = "2c94811c946f6bfb01965b1365c95857"; // TODO: 替换为你的真实AccountSid
    private static final String ACCOUNT_TOKEN = "88af125b33a64040a4e2fa6f4f8b7bf9"; // TODO: 替换为你的真实AuthToken
    // 应用ID
    private static final String APP_ID = "2c94811c946f6bfb01965b13676f585e"; // TODO: 替换为你的真实AppId
    // 模板ID
    private static final String TEMPLATE_ID = "1"; // TODO: 替换为你的真实模板Id

    /**
     * 发送短信验证码
     * @param to 目标手机号
     * @param datas 模板变量（如验证码、有效期等）
     * @return 发送结果
     */
    public boolean sendTemplateSMS(String to, String[] datas) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(SERVER_IP, SERVER_PORT);
        sdk.setAccount(ACCOUNT_SID, ACCOUNT_TOKEN);
        sdk.setAppId(APP_ID);
        sdk.setBodyType(BodyType.Type_JSON);

        // 可选参数
        String subAppend = null; // 扩展码
        String reqId = null;     // 自定义消息id

        HashMap<String, Object> result = sdk.sendTemplateSMS(to, TEMPLATE_ID, datas, subAppend, reqId);

        if ("000000".equals(result.get("statusCode"))) {
            // 正常返回输出data包体信息
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
            return true;
        } else {
            // 异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            return false;
        }
    }
}